package com.service.impl;

import com.bean.MessageBoard;
import com.mapper.MessageBoardMapper;
import com.service.MessageBoardService;
import com.utils.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageBoardServiceImpl implements MessageBoardService {
    @Autowired
    private MessageBoardMapper messageBoardMapper;

    @Override
    public MessageBoard getMessageBoardById(Integer messageId) {
        return messageBoardMapper.getMessageBoardById(messageId);
    }

    @Override
    public PageInfo<MessageBoard> getMessageBoardsWithKeyword(String keyword, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<MessageBoard> messageBoards = messageBoardMapper.getMessageBoardsWithKeyword(keyword, offset, pageSize);
        int total = messageBoardMapper.countMessageBoardsWithKeyword(keyword);
        return new PageInfo<>(pageNum, pageSize, total, messageBoards);
    }

    @Override
    public boolean insertMessageBoard(MessageBoard messageBoard) {
        int result = messageBoardMapper.insertMessageBoard(messageBoard);
        return result > 0;
    }

    @Override
    public boolean updateMessageBoard(MessageBoard messageBoard) {
        int result = messageBoardMapper.updateMessageBoard(messageBoard);
        return result > 0;
    }

    @Override
    public boolean deleteMessageBoardById(Integer messageId) {
        int result = messageBoardMapper.deleteMessageBoardById(messageId);
        return result > 0;
    }
}